package com.xxin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxin.exceptions.BusinessException;
import com.xxin.mapper.sys.SysUsersMapper;
import com.xxin.pojo.dto.LoginDTO;
import com.xxin.pojo.entity.SysUsers;
import com.xxin.pojo.vo.CurrentUserVO;
import com.xxin.pojo.vo.DoctorVO;
import com.xxin.pojo.vo.MenuVO;
import com.xxin.pojo.vo.SysUsersVO;
import com.xxin.service.SysUsersService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * @author 小欣
 * @version 1.0
 * @since 1.8
 */
@Service
public class SysUsersServiceImpl implements SysUsersService {
	
	@Resource
	private SysUsersMapper sysUsersMapper;
	
	@Value("${pageHelper.pageSize}")
	private Integer pageSize;
	
	@Value("${upload.address}")
	private String uploadPath;
	
	private final Map<Integer,String> userTypeMap = new HashMap<>();
	
	{
		userTypeMap.put(1,"系统管理员");
		userTypeMap.put(2,"挂号员");
		userTypeMap.put(3,"门诊医师");
		userTypeMap.put(4,"收银员");
		userTypeMap.put(5,"药方医师");
	}
	
	
	
	@Override
	public CurrentUserVO doLogin(LoginDTO loginDTO) {
		//账户查询
		SysUsers sysUsers = Optional.ofNullable(loginDTO.getAccount()).map(account -> sysUsersMapper.selectByAccount(account)).orElseThrow(() -> new RuntimeException("账户名不存在"));
		//密码校验
		Optional.ofNullable(loginDTO.getPassword()).filter(pwd -> DigestUtils.md5DigestAsHex(pwd.getBytes()).equalsIgnoreCase(sysUsers.getPassword())).orElseThrow(() -> new RuntimeException("密码错误!"));
		//封装VO
		CurrentUserVO currentUserVO = new CurrentUserVO();
		BeanUtils.copyProperties(sysUsers,currentUserVO);
		currentUserVO.setUserTypeName(userTypeMap.get(sysUsers.getUserType()));
		//登录成功返回结果
		return currentUserVO;
	}
	
	@Override
	public String showImg(Long userId) {
		SysUsers sysUsers = Optional.ofNullable(userId).map(id -> sysUsersMapper.selectByPrimaryKey(id)).orElseThrow(() -> new RuntimeException("用户编号不存在!"));
		return sysUsers.getHeadUrl();
	}
	
	@Override
	@SuppressWarnings("all")
	public PageInfo<SysUsersVO> pageQuery(Integer pageNum) {
		//1.开启分页
		PageHelper.startPage(pageNum,pageSize);
		//2.查询数据
		List<SysUsers> sysUsers = sysUsersMapper.selectAll();
		//3.数据封装
		PageInfo sysUsersPageInfo = new PageInfo<>(sysUsers);
		//4.创建一个真实返回数据集合
		List<SysUsersVO> sysUsersVOList = new ArrayList<>();
		//5.数据转VO
		for (SysUsers sysUser : sysUsers) {
			SysUsersVO sysUsersVO = new SysUsersVO();
			BeanUtils.copyProperties(sysUser,sysUsersVO);
			sysUsersVO.setUserTypeName(userTypeMap.get(sysUser.getUserType()));
			sysUsersVOList.add(sysUsersVO);
		}
		//6.将PageInfo中的列表数据改成VO
		sysUsersPageInfo.setList(sysUsersVOList);
		return sysUsersPageInfo;
	}
	
	@Override
	public List<MenuVO> getMenuList(Integer userId) {
		List<MenuVO> list = sysUsersMapper.selectMenuByUserId(userId);
		return list;
	}
	
	@Override
	public void addUser(SysUsers sysUsers, MultipartFile userImg) {
		Optional.ofNullable(sysUsers.getAccount()).filter(account -> sysUsersMapper.selectByAccount(account) == null).orElseThrow(() -> new BusinessException("该用户已存在!",0));
		//对密码进行加密
		sysUsers.setPassword(DigestUtils.md5DigestAsHex(Optional.ofNullable(sysUsers.getPassword()).orElseThrow(() -> new BusinessException("密码不能为空",0)).getBytes()));
		//将数据添加到数据库中
		sysUsersMapper.insertSelective(sysUsers);
	}
	
	@Override
	public void accountExists(String account) {
		SysUsers sysUsers = sysUsersMapper.selectByAccount(account);
		if(null != sysUsers){
			throw new BusinessException("该用户已存在!",0);
		}
	}
	
	@Override
	public List<DoctorVO> getDoctorList(Long departmentId) {
		List<DoctorVO> doctorVOList = sysUsersMapper.selectByDepartmentId(departmentId);
		return doctorVOList;
	}
}
